package cn.edu.jsu.zjj.model.utils;

import cn.edu.jsu.zjj.model.entity.User;
import kotlin.Pair;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;

public class ImportUtils {


    public static Pair<List<String>, List<List<String>>> importExcel(MultipartFile file) {
        List<String> headerList = new ArrayList<>(); // 初始化 userList
        List<List<String>> dataList = new ArrayList<>(); // 初始化 dataList
        try {
            // 获取文件输入流
            InputStream inputStream = file.getInputStream();
            // 定义一个 Workbook 的变量
            Workbook workbook = null;
            // 截取文件名 . 后面的后缀名，判断是 xls 还是 xlsx
            String fileName = file.getOriginalFilename();
            String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();

            if ("xls".equals(fileExtension)) {
                workbook = new HSSFWorkbook(inputStream);
            } else if ("xlsx".equals(fileExtension)) {
                workbook = new XSSFWorkbook(inputStream);
            }

            // 获取第一个 sheet
            Sheet sheet = null;
            if (workbook != null) {
                sheet = workbook.getSheetAt(0);
            }
            // sheet.getPhysicalNumberOfRows() 获取总的行数
            // 循环读取每一行
            if (sheet != null) {
                for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {

                    // 循环读取每一个格
                    Row row = sheet.getRow(i);
                    // row.getPhysicalNumberOfCells() 获取总的列数
                    List<String> oneUser = new ArrayList<>();
                    int numberOfCells = row.getPhysicalNumberOfCells();
                    for (int index = 0; index < numberOfCells; index++) {
                        // 获取数据，但是我们获取的 cell 类型
                        Cell cell = row.getCell(index);
                        // 转换为字符串类型
                        // 获取得到字符串
                        String id = cell.toString();
                        oneUser.add(id);

                    }

                    if (i == 0) {
                        headerList.addAll(oneUser);
                    }else{
                        dataList.add(oneUser);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Pair<List<String>, List<List<String>>> pair = new Pair<>(headerList, dataList);
        return pair;


    }


    private static File MultipartFileToFile(MultipartFile multipartFile) throws IOException {
        if (multipartFile.isEmpty()) {
            throw new IOException("上传文件为空");
        }
        File file = new File(multipartFile.getOriginalFilename());
        multipartFile.transferTo(file);
        return file;
    }
}
